
(function(UI){
	
	//xform
	$.fn.xForm = function(op){
		//判断是否
		if(this[0].tagName == "FORM")
		{
			if(this.hasClass('ccay-widget-body'))
				return this.parent().widget(op);
			else
			{
				op.convertMode = true;
				if(op.customSetting !== false)
				{
					op.layoutItemCss = 'ccay-form-row';
					op.customSettingTarget = '> samp';
				}
			}
			
			return this.widget(op);
		}
		else
		{
			if(this.hasClass('ccay-widget'))
				return this.widget(op);
				
			if(op.tagName != "form")
			{
				var fnCompleted = op.fnComplete;
				op.fnComplete = function(){
					Ccay.UI.InitForm(this.getBody());
				}
			}
			var isInited = UI.XControl.isInited(this);
			!isInited && !op.convertMode && (op = helper.initOp(op));
			var xform = this.widget(op);
			isInited || helper.bindEvents(xform);
			return xform;
		}
	}
	
	var config = {
		body:"<{0} class='init ccay-widget-body ccay-form'></{0}>",
		btns:{
			save:{type:"input",text:"save",attr:{eno:"save"}},
			reset:{type:"input",text:"ccay.common.button.reset",attr:{eno:"reset"}},
			search:{type:"input",css:"default",text:"ccay.common.button.search",attr:{eno:"search"}},
			back:{type:"input",text:"back",attr:{eno:"back"}}
		},
		operation:{
			save:function(e,form){
				if(form.valid())
				{
					var op = form.op;
					op.modal.setData(form.getValue()).saveSingle({
						success:function(result){
							form.proxy(op.fnSave,[result]);
						}
					});
				}
			},
			reset:function(e,form){
				form.setValue({});
			}
		}
	};
	var renderBtns = config.renderBtns = {
		view : [config.btns.back],
		edit : [config.btns.save,config.btns.back],
		search :[config.btns.search,config.btns.reset]
	};
	var helper = {
		initOp:function(op){

			op || (op = {});
			
			op.ajax || (op.ajax = op.modal.findSingle);
			
			op.body = config.body.formatText(op.tagName || "form");
			
			//默认流布局
			if(!op.layout)
			{
				op.layout = "form-Flow"
			}
			else if(op.layout == "form-Table")
			{//table布局暂时不支持个性化
				op.customSetting = false;
			}
			
			//操作方法扩展
			op.operation = $.extend(true,{},config.operation,op.operation);
			
			this.initButton(op);
			op.renderType == "search" && (op.renderType = "edit");
			return op;
		},
		initButton:function(op){
			if(op.innerButton === false)
				return;
			var btns = op.buttons;
			if(btns)
			{
				var i = 0,len = btns.length,btnOp;
				for(;i < len;i++){
					btn = btns[i];
					if(btn.innerBtn)
					{
						btn = btns[i] = $.extend(true,config.btns[btn.innerBtn],btn);
						btn.click && delete btn.attr.eno;
					}
				}
			}
			else
			{
				var renderType = op.renderType || "view";
				op.buttons = renderBtns[renderType];
			}
			return this;
		},
		bindEvents:function(xform){
			xform.getBody().on("click","[eno]",function(e){
				var btn = $(this), eno = btn.attr("eno");
				if(eno){
					var ev = xform.op.operation[eno];
					if(ev)
					{
						ev.call(btn,e,xform);
					}
				}
			})
			//系统已处理
//			.keydown(function(e){
//				if(e.keyCode == 13)
//				{
//					var btn = $(this).find(".default:first");
//					btn.length && btn.click();
//				}
//			})
		}
	};

})(Ccay.UI);
